package com.yunfeng.supercall.activity;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.ly.quickdev.library.adapter.ListBaseAdapter.ViewHolder;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.bean.MyCircleBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;
import com.yunfeng.supercall.manager.ManagerFactory;

/**
 * 创建圈子后，将好友拉入圈子
 * 
 * @author 洋
 * 
 */
public class CreateCirlcleAddFriendActivity extends
		BaseStickyHeaderListActivity<SuperFriendsBean> {

	public boolean[] mChoice;
	private MyCircleBean myCircle;

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSupportContentChanged() {
		// TODO Auto-generated method stub
		super.onSupportContentChanged();
		setTextViewText(R.id.right_text, getString(R.string.done));
		findView(R.id.right_text).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				add();
			}
		});
		setTextViewText(R.id.title, getString(R.string.choose_user));
		try {
			myCircle = getIntent().getParcelableExtra(KEY_DATA);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void add() {
		StringBuilder sb = new StringBuilder();
		for (int j = mChoice.length, i = 0; i < j; i++) {
			if (mChoice[i]) {
				sb.append(mList.get(i).getUserid());
				sb.append(";");
			}
		}
		if (!sb.toString().isEmpty()) {
			sb.deleteCharAt(sb.length() - 1);
		}

		JSONObject json = getBaseParams(getActivity());
		try {
			json.put("i_others", sb.toString());
			json.put("i_circleid", myCircle.getCircleid());
			showProgressDialog(getString(R.string.adding));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(FUNCTION_JOIN_CIRCLE, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub
				showToast(msg);
				cancelProgressDialog();
			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				showToast(R.string.add_success);
				cancelProgressDialog();
				finish();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public void loadData() {
		// TODO Auto-generated method stub
		mList = (List<SuperFriendsBean>) ManagerFactory.createManager(
				SuperFriendsBean.class).getList();
		setListAdapter();
		mChoice = new boolean[mList.size()];
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = getLayoutInflater().inflate(
					R.layout.list_item_friends_selectmode, null);
		}
		ContactsBaseBean.setupSingleLineContactsView(convertView,
				mList.get(position));
		CheckBox box = ViewHolder.get(convertView, R.id.checkbox);
		box.setChecked(mChoice[position]);
		box.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				mChoice[position] = isChecked;
			}
		});
		return convertView;
	}

}
